<!DOCTYPE html>
<title>Test the initial state of AudioContext's with the autoplay policy</title>
<script src='../../resources/testharness.js'></script>
<script src='../../resources/testharnessreport.js'></script>
<script>
async function activateDocument(t) {
  return new Promise((resolve, reject) => {
    chrome.gpuBenchmarking.pointerActionSequence([
      {
        source: 'mouse',
        actions: [
          { name: 'pointerDown', x: 1, y: 1 },
          { name: 'pointerUp' },
        ],
      }
    ], t.step_func(resolve));
  });
}

function setup(t) {
  const autoplayIgnoresWebAudioEnabled =
      internals.runtimeFlags.autoplayIgnoresWebAudioEnabled;

  internals.runtimeFlags.autoplayIgnoresWebAudioEnabled = false;
  internals.settings.setAutoplayPolicy('document-user-activation-required');

  t.add_cleanup(() => {
    internals.runtimeFlags.autoplayIgnoresWebAudioEnabled =
        autoplayIgnoresWebAudioEnabled;
    internals.settings.setAutoplayPolicy('no-user-gesture-required');
  });
}

promise_test(t => {
  setup(t);

  return new Promise((resolve) => {
    const context = new AudioContext();
    assert_equals(context.state, 'suspended');
    resolve();
  });
}, 'AudioContext without document activation is created suspended');

promise_test(t => {
  setup(t);

  return new Promise((resolve) => {
    const context = new OfflineAudioContext(1, 512, 3000);
    assert_equals(context.state, 'suspended');
    resolve();
  });
}, 'OfflineAudioContext without a document action is created suspended');

promise_test(async t => {
  setup(t);

  await activateDocument(t);

  return new Promise((resolve) => {
    const audioContext = new AudioContext();
    assert_equals(audioContext.state, 'running');

    const offlineAudioContext = new OfflineAudioContext(1, 512, 3000);
    assert_equals(offlineAudioContext.state, 'suspended');

    resolve();
  });
}, 'With document actiovation, AudioContext is created running and OfflineAudioContext suspended');
</script>
